package com.titaniume.rabbitmq.springbootrabbitmq.controller;

import com.titaniume.rabbitmq.springbootrabbitmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ProjectName: rabbitmq
 * @Package: com.titaniume.rabbitmq.springbootrabbitmq.controller
 * @ClassName: SendMsgController
 * @Author: titan
 * @Description: TTL 生产者
 * @Date: 2021/12/28 22:43
 * @Version: 1.0
 */
@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("当前时间：{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
        rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: "+message);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
    }

    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    public void sendDelayMsg(@PathVariable String message,@PathVariable Integer delayTime){

        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYPED_EXCHANGE_NAME,DelayedQueueConfig.DELAYPED_ROUTING_KEY,message, messagePostProcessor->{
            messagePostProcessor.getMessageProperties().setDelay(delayTime);
            return  messagePostProcessor;
        });
        log.info("当前时间：{},发送一条延迟:{} 毫秒的信息给队列:{}", new Date(), delayTime,message);
    }
}
